home *** CD-ROM | disk | FTP | other *** search
Lex Description | 1991-12-19 | 3.6 KB | 133 lines |
- %{
- int comments=0;
- int line_numbers=0;
- char *extensions[]={".C",".CPP",".CXX",".H",".HXX",".CC",NULL};
- #include <string.h>
- extern int yyline;
- yyline=1;
- %}
-
- %s COMMENT
- %%
-
- <INITIAL>"/*" {
- if (line_numbers)
- fprintf(yyout,"%.4d:",yyline);
- comments ++;
- BEGIN COMMENT;
- }
-
- <COMMENT>. {
- fprintf(yyout,"%c",*yytext);
- }
-
- <COMMENT>\n {
- yyline++;
- fprintf(yyout,"\n");
- if (line_numbers)
- fprintf(yyout,"%.4d:",yyline);
- }
-
- <COMMENT>"*/" {
- fprintf(yyout,"\n");
- BEGIN INITIAL;
- }
-
- <INITIAL>"*/" {
- fprintf(yyout,"\n\nFound close comment outside of comment.\n\n");
- }
-
- <INITIAL>\"(\\.|[^\"])*\" ; /* This recognizes and ignores string constants */
- <INITIAL>'[^\\]'|'\\[0-0xFF]' ; /* This does the same for character constants */
- <INITIAL>"//".*$ {
- char *line=yytext+2;
- fprintf(yyout,"%s\n",line);
- }
-
- <INITIAL>. ;
- <INITIAL>\n yyline++;
-
- %%
- int main(int argc,char **argv) {
-
- char file_name[_MAX_PATH];
- char **extension;
- int found=0;
- char *name_end;
- char output_ext[5]="\0";
- char output_file[_MAX_PATH]="\0";
-
- while (argv[1][0]=='-') {
- switch(argv[1][1]) {
- case 'l':
- case 'L':
- line_numbers=1;
- break;
- case 'e':
- case 'E':
- strcpy(output_ext,argv[1]+2);
- break;
- case 'o':
- case 'O':
- strcpy(output_file,argv[1]+2);
- break;
- default:
- fprintf(stderr,"Usage:getcmt [-l][-?][filename [filename...]]"
- "\n-l: add line numbers before comments"
- "\n-?: Show this help"
- "\n filenames may include normal DOS wildcards");
- return 0;
- }
- argc--;
- argv++;
- }
-
- if (argc<2) {
- /* read standard input if no file name supplied */
- yyline=0;
- yylex();
- }
- else while (--argc) {
-
- comments=0;
- yyline=1;
- strcpy(file_name,*++argv);
- name_end=strchr(file_name,'\0');
- /* otherwise, open each file supplied in turn. */
- if ((yyin=fopen(file_name,"r"))==NULL && strchr(*argv,'.')==NULL) {
- for (found=0,extension=extensions;*extension;extension++) {
- strcpy(name_end,*extension);
- if ((yyin=fopen(file_name,"r"))!=NULL) {
- found=1;
- break;
- }
- }
- if (!found) {
- fprintf(yyout,"\n\n\tError: Unable to open %s\n",*argv);
- continue;
- }
- }
- if (*output_ext) {
- strcpy(output_file,file_name);
- name_end=strchr(output_file,'.')+1;
- strcpy(name_end,output_ext);
- if ((yyout=fopen(output_file,"a"))==NULL)
- yyout=stdout;
- }
- else if (*output_file) {
- if ((yyout=fopen(output_file,"a"))==NULL)
- yyout=stdout;
- }
- fprintf(yyout,"\n\n\t%s\n",file_name);
- yylex(); /* Do the real work. */
- fprintf(yyout,"\n");
- yyrestart(yyin);
- if (yyin !=stdin)
- fclose(yyin);
- fclose(yyin);
- if (yyout!=stdout)
- fclose(yyout);
- }
- return comments;
- }
-